@echo off
REM
REM      Name: diagnostic.bat  [run Widget Framework(WGF) diagnostic.bat to diagnostic and automatic fix WGF]
REM
REM      Use this batch file to run WGF diagnotic non-interactively
REM      to diagnostic and fix WGF problem automatically.
REM
REM
REM Syntax: diagnostic.bat [/h|/?] [/user strUserNameOfFolder]
REM
REM
REM Optional Command Line Switches:
REM
REM '/h' or '/?' Help => If specified, displays the help text.
REM
REM '/user strUserNameOfFolder' If specify strUserNameOfFolder, it will be used strUserNameOfFolder
REM                             granted the permissions to WGF folder.
REM              				Default strUserNameOfFolder = Everyone
REM              				Recommend strUserNameOfFolder = IUSR
REM              				(eg. "/user=IUSR").
REM

:CHECKFORSWITCHES
IF '%1'=='/h'        GOTO DISPLAY_HELP
IF '%1'=='/H'        GOTO DISPLAY_HELP
IF '%1'=='/?'        GOTO DISPLAY_HELP
IF '%1'=='/iusername' GOTO SET_ACE ELSE GOTO SET_DEFAULT_ACE
IF '%1'=='' GOTO SET_DEFAULT_ACE
GOTO BEGIN_CODE

:DISPLAY_HELP
echo.
echo ----------------------------------------------------------------------------
echo   Name: diagnostic.bat  
echo   Descripton: run diagnostic.bat to diagnostic and automatic fix WGF
echo ----------------------------------------------------------------------------
echo. 
echo. 
echo ----------------------------------------------------------------------------
echo  Syntax: diagnostic.bat [/h^|/?] [/iusername strIUserNameForFolder]
echo.
echo  Optional Command Line Switches:
echo ----------------------------------------------------------------------------
echo  '/h' or '/?' 
echo  ^=^> ^If specified, displays the help text.
echo. 
echo  '/iusername strIUserNameForFolder' 
echo  ^=^> ^If specify strUserNameOfFolder, it will be used strIUserNameForFolder 
echo     granted the permissions to WGF folder.
echo     Default strUserNameOfFolder = Everyone
echo     Recommend strUserNameOfFolder = IUSR
echo     (eg. "/iusername=IUSR").
echo. 
GOTO END

:SET_DEFAULT_ACE
set ACE="n:S-1-1-0;p:full;s:y"
set ACE_SHOW="user:Everyone;Permissions:full"
GOTO BEGIN_CODE

:SET_ACE
REM set ACE="n:%2;p:read,write,list_folder,read_ex,change;s:n"

set ACE_EVERYONE="n:S-1-1-0;p:read,read_ex;s:y"
set ACE_EVERYONE_SHOW=user:Everyone;Permissions:Read ^& ^Execute,List Folder Contents,Read

set ACE="n:%2;p:read,write,read_ex,change;s:n" -ace %ACE_EVERYONE%
set ACE_SHOW="user:%2;Permissions:Modify,Read & Execute,List Folder Contents,Read,Write && %ACE_EVERYONE_SHOW%"
SHIFT
GOTO CHECKFORSWITCHES

:BEGIN_CODE
echo.
echo -----------------------------------------------------
echo Setting environment variables
echo -----------------------------------------------------
echo.

Set RegQry=HKLM\Hardware\Description\System\CentralProcessor\0
REG.exe Query %RegQry% > CheckOS.txt
Find /i "x86" < CheckOS.txt > StringCheck.txt
If %ERRORLEVEL% == 0 (
	echo "This is 32 Bit Operating system"
    GOTO X86
) ELSE (
	echo "This is 64 Bit Operating system"
    GOTO X64
)

:X86
set MY_PROGRAMFILES=%PROGRAMFILES%
set BIT=x86
GOTO END_OS_BIT

:X64
set MY_PROGRAMFILES=%PROGRAMFILES(X86)%
set BIT=x64
GOTO END_OS_BIT

:END_OS_BIT
call SetEnv.bat

IF NOT EXIST "%WGF_SRC%" echo. && echo [ERROR] && echo. && echo %WGF_SRC% folder does not exists, please set the correct value in %WGF_DIAGNOSTIC%\SetEnv.bat and try again && GOTO FAILED 
echo WGF_SRC=%WGF_SRC%

IF NOT EXIST "%WINDOWS_TMP%" echo. && echo [ERROR] && echo. && echo %WINDOWS_TMP% folder does not exists, please set the correct value in %WGF_DIAGNOSTIC%\SetEnv.bat and try again && GOTO FAILED 
echo WINDOWS_TMP=%WINDOWS_TMP%

IF NOT EXIST "%WGF_DIAGNOSTIC%" echo. && echo [ERROR] && echo. && echo %WGF_DIAGNOSTIC% folder does not exists, please set the correct value in %WGF_DIAGNOSTIC%\SetEnv.bat and try again && GOTO FAILED 
echo WGF_DIAGNOSTIC=%WGF_DIAGNOSTIC%

set SETACL_EXE="%WGF_DIAGNOSTIC%\SetACL\Command line version\%BIT%\SetACL.exe"
echo SETACL_EXE=%SETACL_EXE%

echo ACE=%ACE%
echo ACE_SHOW=%ACE_SHOW%

echo.
echo -----------------------------------------------------
echo Remove tmp files
echo -----------------------------------------------------
echo.
IF EXIST "%WGF_DIAGNOSTIC%\log\setacl_log.txt" DEL "%WGF_DIAGNOSTIC%\log\setacl_log.txt"
IF EXIST "%WGF_DIAGNOSTIC%\CheckOS.txt" DEL "%WGF_DIAGNOSTIC%\CheckOS.txt"
IF EXIST "%WGF_DIAGNOSTIC%\StringCheck.txt" DEL "%WGF_DIAGNOSTIC%\StringCheck.txt"

echo.
echo -----------------------------------------------------
echo Grant read/write permissions to %WGF_SRC%\repository
echo -----------------------------------------------------
echo.

%SETACL_EXE% -on "%WGF_SRC%\repository" -ot file -actn ace -ace %ACE% -rec cont_obj -log "%WGF_DIAGNOSTIC%\log\setacl_log.txt" -silent

echo.
echo -----------------------------------------------------
echo Grant read/write permissions to %WINDOWS_TMP%
echo -----------------------------------------------------
echo.

%SETACL_EXE% -on "%WINDOWS_TMP%" -ot file -actn ace -ace "n:S-1-1-0;p:full;s:y" -rec no -log "%WGF_DIAGNOSTIC%\log\setacl_log.txt" -silent
:SUCCESS
echo.
echo Diagnostic and automatic fix WGF success!
GOTO END

:FAILED
echo.
echo Diagnostic and automatic fix WGF failed!
GOTO END

:END